<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\WebPage;
use StudyBuddy\Template\Urhere;

/**
 * Controller for rendering
 * the "Members" page
 *
 * If request is by Ajax, it returns only the content
 * of the main area, paginated, sorted and with pagination
 * links if necessary
 *
 */
class Viewrecontent extends WebPage {

    protected $pagerPath = '/content/about';
    protected $layoutID = 1;
    protected $qtab = '';

    protected function main() {

        $this->setTitle()
                ->makeTopTabs()
                ->makeContentBody()
                ->makeRecentActivites()
                ->makeRecentTags()
                ->makeTutorListing();
    }

    protected function setTitle() {
        $title = 'About';
        $this->page = $this->oRequest->get('page', 's', '');
        if($this->page == 'terms') {
            $title = 'Terms';
        } else if($this->page == 'help') {
            $title = 'Help';
        } else if($this->page == 'top-10-reasons') {
            $title = 'Top 10 reasons';
        }
        $this->aPageVars['title'] = $title;
        $this->aPageVars['qheader'] = '<h1>' . $title . '</h1>';

        return $this;
    }

    protected function makeTopTabs() {
        $guest = null;
        if($this->oRegistry->Viewer->isGuest()) {
            $guest['link_ask'] = '#'; // 13
            $guest['link_ask_click'] = ' onclick="oSL.getQuickRegForm()"'; //14
        }
        d('cp');
        $tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', $this->qtab, $guest);
        $this->aPageVars['topTabs'] = $tabs;

        return $this;
    }
    
    protected function makeRecentTags() {

        $aUserTags = $this->oRegistry->Viewer['a_f_t'];
        if (!empty($aUserTags)) {
            $s = $this->getSortedRecentTags($aUserTags);
        } else {
            $s = $this->oRegistry->Cache->get('qrecent');
        }

        $tags = \tplBoxrecent::parse(array('tags' => $s, 'title' => $this->_('Recent Tags')));
        d('cp');
        $this->aPageVars['tags'] = $tags;

        return $this;
    }

    //hoangto tutor listing
    protected function makeTutorListing() {

        $s = $this->getSortedTutors();
        $tutors = \tplTutorBox::parse(array('tutors' => $s, 'title' => $this->_('Tutor Listing')));
        d('cp');
        $this->aPageVars['tutors'] = $tutors;

        return $this;
    }

    //hoangto tutor listing
    protected function makeRecentActivites() {

        $s = $this->getSortedActivities();
        $tutors = \tplActivityBox::parse(array('ractivites' => $s, 'title' => $this->_('Recent activities')));
        d('cp');
        $this->aPageVars['activities'] = $tutors;

        return $this;
    }
    
    protected function getSortedRecentTags(array $aUserTags, $type = 'recent') {

        $limit = 9;
        if ('unanswered' === $type) {
            //$cur = $this->oRegistry->Mongo->UNANSWERED_TAGS->find(array(), array('tag', 'i_count'))->sort(array('i_ts' => -1))->limit($limit);
            $cur = $this->oRegistry->Mongo->UNANSWERED_TAGS->find(array(), array('tag', 'i_count'))->sort(array('i_count' => -1))->limit($limit);
        } else {
            //$cur = $this->oRegistry->Mongo->QUESTION_TAGS->find(array('i_count' => array('$gt' => 0)), array('tag', 'i_count'))->sort(array('i_ts' => -1))->limit($limit);
            $cur = $this->oRegistry->Mongo->QUESTION_TAGS->find(array('i_count' => array('$gt' => 0)), array('tag', 'i_count'))->sort(array('i_count' => -1))->limit($limit);
        }

        d('got ' . $cur->count(true) . ' tag results');
        $aTags = iterator_to_array($cur);

        d('aTags: ' . print_r($aTags, 1));

        if (!empty($aTags)) {
            usort($aTags, function($a, $b) use ($aUserTags) {
                        return (in_array($a['tag'], $aUserTags)) ? -1 : 1;
                    });
        };

        d('$aTags now: ' . print_r($aTags, 1));
        $html = ('unanswered' === $type) ? \tplUnanstags::loop($aTags) : \tplLinktag::loop($aTags);

        d('html recent tags: ' . $html);

        return '<div class="tag"><ul>' . $html . '</ul></div>';
    }

    //hoangto tutor listing loop
    protected function getSortedTutors() {

        $cur = $this->oRegistry->Mongo->USERS->find(array('tmp_role' => 'tutor'))->sort(array('experience' => -1))->limit(9);

        $canTeach = '';

        $func = function(&$a) use($canTeach) {
                    $a['canTeach'] = $canTeach;
                };

        $tutors = \tplTutorListing::loop($cur, true, $func);

        return $tutors;
    }

    //hoangto tutor listing loop
    protected function getSortedActivities() {

        $act_link = '';
        $badges = '';

        $func = function(&$a) use($act_link, $badges) {
                    $a['act_link'] = $act_link;
                    $a['badges'] = $badges;
                };

        $cur = $this->oRegistry->Mongo->ACTIVITIES->find()->sort(array('i_ts' => -1))->limit(5);

        $tutors = \tplActivityList::loop($cur, true, $func);

        return $tutors;
    }

    protected function makeContentBody() {
        $aContent[0] = '';
        if($this->page == 'about') {
            $aContent[0] = '<div class="subtitle">About Us</div>
                    <div id="grid1">
                        <div class="row1">
                            <div class="cell1" style="width: 60px;" align="center">
                                <img src="/images/question.png"></div>
                            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                                 <p><strong>Credible, Relevant, Swift</strong><br>
                                        Studybuddy aims to be most credible revision hub for all students.
                                        Come to StudyBuddy to find the latest notes and study tips or simply 
                                        ask a question!</p>
                                <p>We find only the best tutors to be our consultants to maintain 
                                    the high standards of StudyBuddy\'s answers. Our promise to you is that 
                                    you will receive an answer within 1 hour of posting the question between
                                    8am to 12mn 				daily. During crunch time, we will burn the midnight 
                                    oil with you and make sure you understand the keys to unlock all 
                                    problems.</p>
                            </div>
                        </div>
                        <div class="row1">
                            <div class="cell1" style="width: 60px;" align="center">
                                <img src="/images/present.png"></div>
                            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                                 <p><strong>Free for all students</strong><br>
                                        By providing efficient, relevant and credible assistance to all 
                                        students, we intend to achieve a fair education opportunity for 
                                        everyone. Tuition fees are not getting cheaper in Singapore, and to stay
                                        competitive, most parents have spent thousands of dollars for their 
                                        kids\' tuition.<br>
                                            <br>
                                                Tired of travelling and wasting valuable resting time just to ask 
                                                tutors a few questions ? Fret not! We\'re here from 8am to 2am daily and 
                                                we are as good as your tutors (or better) ! When you revise at home, you
                                                just have one question which you are not sure of, and the objective 
                                                answer at the back of the textbook just does not help.</p>
                                                <p>Welcome to Studybuddy.<br>
                                                </p></div>
                                                </div>
                                                <div class="row1">
                                                    <div class="cell1" style="width: 60px;" align="center">
                                                        <img src="/images/cloud.png"></div>
                                                    <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                                                         <strong>What we aspire to be</strong><br>
                                                            A personalised platform for everyone to do everything related to their
                                                            school stuff, to help them to be more efficient in learning and 
                                                            competent in studies. Revision notes, schedules, reminders, quizzes, 
                                                            video lectures are some of the sneak preview functions which we will add
                                                            on in future.<p></p>
                                                    </div>
                                                </div>
                                                <div class="row1">
                                                    <div class="cell1" style="width: 60px;" align="center">
                                                        <img src="/images/pen.png"></div>
                                                    <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                                                         <p><strong>Partnership with Schools &amp; Tuition Agencies</strong><br>
                                                                As we would like to help as much students as possible, if you\'re 
                                                                interested to connect with us to reach out to as many students as 
                                                                possible, you can email us at <a href="mailto:hello@studybuddy.com.sg">hello</a><a href="mailto:hello@studybuddy.com.sg">@</a><a href="mailto:hello@studybuddy.com.sg">studybuddy</a><a href="mailto:hello@studybuddy.com.sg">.</a><a href="mailto:hello@studybuddy.com.sg">com</a><a href="mailto:hello@studybuddy.com.sg">.</a><a href="mailto:hello@studybuddy.com.sg">sg</a> and we can have a cuppa.</p>
                                                    </div>
                                                </div>
                                                <div class="row1">
                                                    <div class="cell1" style="width: 60px;" align="center">
                                                        <img src="/images/shout.png"></div>
                                                    <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                                                         All symbols used on the About Us Page and Top 10 reasons were by various contributors, from thenounproject.com collection.
                                                </div>
                                            </div>
                                            </div>';
        } else if($this->page == 'terms') {
            $aContent[0] = 'Terms';
        } else if($this->page == 'help') {
            $aContent[0] = 'Help';
        } else if($this->page == 'top-10-reasons') {
            $aContent[0] = '<div class="subtitle">The biggest 10 reasons why students use Studybuddy!</div>
    <div id="grid1">
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/like.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Top quality answers to your questions - Expect nothing less than the best answers</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/cheer.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Great free resources - study guides, model answers for exam papers, personal study schedules and so much more.<br>
                </p></div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/friends.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">  Great companion - you think you\'re alone in this fight ? No. We\'re all here for you.</div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/old.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Experienced and up-to-date consultants - 5-10 years as tutors</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/money.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Free for all - free always</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/24-7.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>24/7 Support</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/broadcast_tower.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Access anywhere - Harness the power of the internet</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/juggle.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Be motivated by peers!</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/seasaw.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Great fun - learn with your friends and never feel the same about revising again.</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/mushroom.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Real world rewards - we dont just give you a digital badge</p>
            </div>
        </div>
        <div class="row1">
            <div class="cell1" style="width: 60px;" align="center">
                <img src="/images/transfer.png"></div>
            <div class="cell1" style="width: 500px;" vertical-align:="" "top;"="">
                 <p>Social factor - If this works, we want to give back part of our revenue to the Pocket Money Fund</p>
            </div>
        </div>
    </div>';
        }
        
        $sContent = \tplContent::parse($aContent, false);
        $this->aPageVars['body'] = $sContent;
        d('cp');
        /**
         * In case of Ajax can just send out sQlist as result
         */
        return $this;
    }

}
